<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class article_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        $this->page_title = 'Thông tin';
        $this->table_name = 'article';
        $this->route_name = 'article';
        $this->business_model = $this->article;
    }

    public function asset()
    {
        $uri = $this->uri->segment(2,false) . '/' . $this->uri->segment(3,false);
        $id = $this->business_model->get_element_by_uri($uri)->id;
        redirect(base_url() . 'ci-admin/article/update/'.$id);
    }

    public function get_post_data()
    {
        $file_path = $this->upload_image('new_file',$this->input->post('file_path'),'uploads/');

        if($this->input->post('removeFile') == 'YES'){
            $this->delete_file($file_path);
            $file_path = '';
        }

        $parentArticle = $this->article->get_element_by_id($this->input->post('parent'));
        if($parentArticle->lang_mapping == '' || $parentArticle->lang_mapping == null || $parentArticle->lang_mapping == '0'){
            $parentArticle2 = $this->business_model->get_element_by_lang_mapping($parentArticle->id);

        }else{
            $parentArticle2 = $this->business_model->get_element_by_id($parentArticle->lang_mapping);
        }

        $parent_string1 = '';
        $parent_string2 = '';
        if (strpos($parentArticle->url,'investment-management') !== false) {
            if(strpos($this->input->post('url'),'investment-management') !== false ){
                $parent_string1 = $parentArticle->id;
                $parent_string2 = $parentArticle2->id;

            }else{
                $parent_string2 = $parentArticle->id;
                $parent_string1 = $parentArticle2->id;
            }

        }else{

            if(strpos($this->input->post('url'),'investment-management') === false ){
                $parent_string2 = $parentArticle->id;
                $parent_string1 = $parentArticle2->id;
            }else{
                $parent_string1 = $parentArticle->id;
                $parent_string2 = $parentArticle2->id;
            }
        }

        $post_data = array(
            'file_path' => $file_path,
            'parent' => $parent_string1,
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content'),
            'url' => $this->input->post('url'),
            'lang_mapping' => $this->input->post('lang_mapping'),
            'display_type' => $this->input->post('display_type'),
            'modified_date' => time()
        );

        $post_data1 = array(
            'file_path' => $file_path,
            'parent' => $parent_string2,
            'title' => $this->input->post('title1'),
            'content' => $this->input->post('content1'),
            'url' => $this->input->post('url1'),
            'lang_mapping' => $this->input->post('lang_mapping1'),
            'display_type' => $this->input->post('display_type'),
            'modified_date' => time()
        );
        return array($post_data,$post_data1);
    }

    public function save($districtid)
    {
        $post_data = $this->get_post_data();
        if($districtid == -1)
        {
            $this->db->insert($this->table_name,$post_data[0]);
            $districtid = $this->db->insert_id();

            $post_data[1]['lang_mapping'] = $districtid;
            $districtid = $this->db->insert($this->table_name,$post_data[1]);
        }
        else
        {
            $model = $this->business_model->get_element_by_id($districtid);
            if($model->lang_mapping == '' || $model->lang_mapping == null || $model->lang_mapping == '0'){
                $model1 = $this->business_model->get_element_by_lang_mapping($districtid);

            }else{
                $model1 = $this->business_model->get_element_by_id($model->lang_mapping);
            }

            for($i=0 ; $i<count($post_data) ; $i++){
                if($model->lang_mapping == $post_data[$i]['lang_mapping']){
                    $this->db->update($this->table_name,$post_data[$i],array('id' => $model->id));
                }else{
                    $this->db->update($this->table_name,$post_data[$i],array('id' => $model1->id));
                }
            }

        }
        $this->session->set_userdata('message',$this->save_notification);
        redirect($this->hook_redirect_url_after_save($districtid));
    }

    public function get_list_actions()
    {
           $actions = array();

           $actions[] = array(
               'text' => 'Thêm',
               'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/-1',
               'view_change_method' => 'redirect'
           );
           $actions[] = array(
               'text' => 'Xóa',
               'href' => base_url(). $this->base_admin_url .$this->route_name.'/delete',
               'view_change_method' => 'submit'
           );
           return $actions;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach($news as $item)
        {
            $actions = array();
//            $actions[] = array(
//                'text' => 'Sub News',
//                'href' => base_url(). $this->base_admin_url .'article_child_controller/index/'.$item->id
//            );
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );
            $list[] = array(
                'can_delete' => $item->id,
                'title' => $item->title,
                'content' => substr_words($item->content,100),
                'url' => $item->url,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'Tiêu đề';
        $list[] = 'Nội dung';
        $list[] = 'Đường dẫn';
        $list[] = 'Công việc';
        return $list;
    }

    public function get_fields($id)
    {

        if($id == -1)
        {
            $model = new $this->business_model();
            $model->title = '';
            $model->content = '';
            $model->url = '';
            $model->parent = '';
            $model->lang_mapping = '';

            $model1 = new $this->business_model();
            $model1->title = '';
            $model1->content = '';
            $model1->url = '';
            $model1->parent = '';
            $model1->lang_mapping = '';
        }
        else
        {
            $model = $this->business_model->get_element_by_id($id);
            if($model->lang_mapping == '' || $model->lang_mapping == null || $model->lang_mapping == '0'){
                $model1 = $this->business_model->get_element_by_lang_mapping($id);

            }else{
                $model1 = $this->business_model->get_element_by_id($model->lang_mapping);
            }


        }

        $fields = array();

        $item = new article();
        $item->title = '.....';
        $articles = $this->article->get_other_elements($id);
        $parents = array_merge(array($item),$articles);
        $fields[] = array(
            'text' => 'Thư mục cha',
            'name' => 'parent',
            'type' => 'select',
            'options' => $parents,
            'select_value' => 'id',
            'select_display' => 'title',
            'value' => $model->parent
        );

        //========== language 1
        $fields[] = array(
            'text' => 'Tiêu đề',
            'name' => 'title',
            'value' => $model->title,
            'type' => 'text'
        );
        $fields[] = array(
        'text' => 'Nội dung',
            'name' => 'content',
            'value' => $model->content,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Đường dẫn',
            'name' => 'url',
            'value' => $model->url,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Đường dẫn',
            'name' => 'lang_mapping',
            'value' => $model->lang_mapping,
            'type' => 'hidden'
        );

        $fields[] = array(
            'text' => '',
            'name' => '',
            'value' => '======================= Ngôn ngữ khác =============================',
            'type' => 'label'
        );

        //========== language 2
        $fields[] = array(
            'text' => 'Tiêu đề',
            'name' => 'title1',
            'value' => $model1->title,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Nội dung',
            'name' => 'content1',
            'value' => $model1->content,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Đường dẫn',
            'name' => 'url1',
            'value' => $model1->url,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Đường dẫn',
            'name' => 'lang_mapping1',
            'value' => $model1->lang_mapping,
            'type' => 'hidden'
        );

        $fields[] = array(
            'text' => '',
            'name' => 'file_path',
            'value' => $model->file_path,
            'type' => 'hidden'
        );
        $fields[] = array(
            'text' => '',
            'name' => 'removeFile',
            'value' => '',
            'type' => 'hidden'
        );

        $jqueryString = '
            <input id="remove_button" type="button" value="Remove"  />
            <script> $(function(){

                $("#remove_button").click(function(){
                    $(this).hide();
                    $("input[name=removeFile]").val("YES");
                    $("#file_label").text("Will be removed");
                });
            }); </script>
        ';

        $fields[] = array(
            'text' => 'Tập tin hiện tại',
            'name' => 'current_file',
            'value' => '<span id="file_label">' .$model->file_path . '</span>' . (($model->file_path != '') ? $jqueryString : ''),
            'type' => 'label'
        );
        $fields[] = array(
            'text' => 'Tải tập tin (pdf hoặc microsoft word)',
            'name' => 'new_file',
            'value' => '',
            'type' => 'file'
        );

        $fields[] = array(
            'text' => 'Kiểu hiển thị',
            'name' => 'display_type',
            'value' => $model->display_type,
            'type' => 'select',
            'options' => array(
                array_to_object(array(
                    'text' => 'Danh sách',
                    'value' => 'LIST'
                )),
                array_to_object(array(
                    'text' => 'Bài viết',
                    'value' => 'ARTICLE'
                ))
            ),
            'select_value' => 'value',
            'select_display' => 'text'
        );
        return $fields;
    }
}